<template>
    <div class="image-dialog">
        <div class="image-dialog-trigger" :class="loaded==true?'hidden':''" type="button" @click="showDialog">
            <div ref="thumb" class="input-wrapper">
                <div class="input">
                    <i class="iconfont icon-party"><img src="../assets/img/u185.png" alt=""></i>
                    <span class="placeholder">{{placeholderString}}</span>
                    <i class="iconfont icon-search">
                        <img src="../assets/img/u186.png" alt="">
                    </i>
                </div>
            </div>
        </div>

        <div class="image-dialog-background" v-show="appearedDialog" ref="dialog">
            <div ref="full" @load="onLoadFull" class="input-wrapper image-dialog-full">
                <div class="title">
                    <i class="iconfont icon-return" @click="turnBack"> <img src="../assets/img/u207.png" alt=""></i>
                    <i class="iconfont icon-party"><img src="../assets/img/u185.png" alt=""></i>
                    <span class="placeholder">{{placeholderString}}</span>
                </div>
            </div>
            <div class="search_box">
                <input type="text" class="search" v-model="theme" :placeholder="inputPlaceholder">
                <button @click="handlerSearch">搜索</button>
            </div>



            <div class="main-body" v-if="pageType!=='study'">
                <mt-loadmore :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore"
                             :autoFill="isAutoFill">
                    <ul class="mui-table-view mui-grid-view" v-if="listData.length>0">
                        <li v-for="item in listData" :key="item.id" @click="turnToPage(item.id)">
                            <div class="img_box">
                                <img :src="item.attachments " alt="">
                            </div>
                            <div class="content ">
                                <h3>{{item.theme}}</h3>
                                <p class="detail">{{item.description }}</p>
                                <p class="time">{{item.activityDate}}</p>
                            </div>
                        </li>
                    </ul>
                </mt-loadmore>
            </div>

            <!--学习搜索-->
            <div class="main-body" v-if="pageType==='study'&&listData.length>0">
                <mt-loadmore :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore"
                             :autoFill="isAutoFill">
                    <ul class="mui-table-view mui-grid-view" v-if="resultData[b]">
                        <li class="type" style="margin-left: 0.26rem" >党建微课堂</li>
                        <li v-for="item in resultData[a]" :key="item.id" @click="turnToPage(item.id)">
                            <div class="img_box">
                                <img :src="item.attachments " alt="">
                            </div>
                            <div class="content ">
                                <h3>{{item.theme}}</h3>
                                <p class="detail">{{item.description }}</p>
                                <p class="time">{{item.activityDate}}</p>
                            </div>
                        </li>
                    </ul>
                    <ul class="pole" v-if="resultData[b]">
                        <li class="type">优秀标杆</li>
                            <li v-for="item in resultData[b]" :key="item.id"  @click="turnToPage(item.id)">
                            <div>
                                <img :src="item.attachments " alt="">
                            </div>
                            <div class="content ">
                            <h3>{{item.theme}}</h3>
                            <p class="details">{{item.description }}</p>
                            <p class="time">{{item.activityDate}}</p>
                            </div>
                        </li>
                    </ul>
                    <ul class="pole" v-if="resultData[c]">
                        <li class="type">在线学习</li>
                            <li v-for="item in resultData[c]" :key="item.id"  @click="turnToPage(item.id)">
                            <div class="content ">
                            <h3>{{item.theme}}</h3>
                            <p class="details">{{item.description }}</p>
                            <p class="time">{{item.activityDate}}</p>
                            </div>
                        </li>
                    </ul>
                    <ul class="mui-table-view mui-grid-view" v-if="resultData[d]">
                        <li class="type" style="margin-left: 0.26rem" >学习心得</li>
                        <li v-for="item in resultData[d]" :key="item.id" @click="turnToPage(item.id)">
                            <div class="img_box">
                                <img :src="item.attachments " alt="">
                            </div>
                            <div class="content ">
                                <h3>{{item.theme}}</h3>
                                <p class="detail">{{item.description }}</p>
                                <p class="time">{{item.activityDate}}</p>
                            </div>
                        </li>
                    </ul>
                </mt-loadmore>
            </div>
        </div>
    </div>
</template>

<script>
    import BASE_URL from '@/api/url'
    import {pullDown} from '@/views/common/mixin'
    import {groupBy} from '@/lib/js/tools'

    export default {
        name: "searchBar",
        mixins: [pullDown],
        props: {
            thumb: String,
            full: String,
            radius: Number,
            placeholder: String,
            pageParam: Object,
            url: String,
            inputPlaceholder:String
        },
        data() {
            return {
                loaded: false,
                appearedDialog: false,
                fullWidth: 0,
                fullHeight: 0,
                listData: [],
                allLoaded: false,
                isAutoFill: false,
                theme: '',
                placeholderString: this.$props.placeholder,
                pageType:null,
                resultData:{},
                a:'"01"',
                b:'"02"',
                c:'"03"',
                d:'"04"',
            }
        },
        created() {
            this.fullWidth = window.screen.width;
            this.fullHeight = window.screen.height;



        },
        mounted() {
            this.pageType = this.$route.query.pageType

        },
        methods: {
            showDialog() {
                this.placeholderString = `${this.placeholder}搜索`;
                this.appearedDialog = true;
                document.title = this.placeholderString;
                this.listData = []
            },
            onLoadFull() {
                this.loaded = true;
            },
            turnBack() {
                this.appearedDialog = false;
                document.title = this.placeholder;
                this.placeholderString = this.placeholder;
            },
            handlerSearch() {
                this.pageParam.page = 1;
                this.pageParam.theme = this.theme;
                this.fetchData();
                this.placeholderString = this.placeholderString.substring(0, this.placeholderString.length - 2);
                document.title = `"${this.theme}"的搜索结果-${this.placeholderString}`;
                this.placeholderString = `关键词"${this.theme}"的搜索结果`
            },
            fetchData() {
                this.$get(this.url, this.pageParam).then(data => {
                    this.totalNum = data.data.totalNum;
                    if (this.totalNum < data.data.currentPage * data.data.pageSize) {
                        this.allLoaded = true; // 若数据已全部获取完毕
                    }
                    this.listData = data.data.items.map((item) => {
                        return {
                            activityDate: item.activityDate,
                            attachments: `${BASE_URL}/${item.attachments}`,
                            browseCount: item.browseCount,
                            id: item.id,
                            theme: item.theme,
                            description: item.description,
                            praiseCount: item.praiseCount,
                            type:item.type,
                        }
                    });
                    this.resultData = groupBy(this.listData, (item) => {
                        return item.type
                    })
                })
            },


        }
    }
</script>

<style lang='less' scoped>
    *,
    *::before,
    *::after {
        box-sizing: border-box;
    }

    .image-dialog {
        width: 100%;
        .image-dialog-trigger {
            width: 100%;
            &.hidden {
                opacity: 0;
            }
        }
        .image-dialog-full {
            display: flex;
        }
        .input-wrapper {
            background: linear-gradient(135deg, rgba(240, 83, 82, 1) 0%, rgba(240, 83, 82, 1) 0%, rgba(215, 36, 8, 1) 100%, rgba(215, 36, 8, 1) 100%);;
            box-sizing: border-box;
            height: 1rem;
            display: inline-flex;
            justify-content: center;
            width: 100vw;
            .input {
                border: none;
                outline: none;
                font-size: 15px;
                border-radius: 5px;
                background: linear-gradient(135deg, rgba(240, 83, 82, 1) 0%, rgba(240, 83, 82, 1) 0%, rgba(215, 36, 8, 1) 100%, rgba(215, 36, 8, 1) 100%);
                display: flex;
                flex-direction: row;
                align-items: center;
                flex: 1;

                input {
                    border: none;
                    outline: none;
                    font-size: 15px;
                    border-radius: 5px;
                    padding-left: 0;
                    flex: 1;

                }
                .icon-party {
                    img {
                        width: 0.5rem;
                        height: 0.5rem;
                        margin-left: 0.16rem;
                    }

                }
                .icon-search {
                    margin-left: auto;
                    /*align-self: stretch;*/
                    img {
                        width: 0.38rem;
                        height: 0.38rem;
                        margin-right: 0.36rem;
                    }
                }
                .placeholder {
                    color: #fff;
                    font-size: 0.28rem;
                    margin-left: 0.14rem;

                }
            }
        }

    }

    .image-dialog-trigger {
        margin: 0;
        padding: 0;
        background: none;
        border: none;
        cursor: pointer;
    }

    .image-dialog-background {
        overflow: auto;
        position: fixed;
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
        width: 100vw;
        height: 100vh;
        background-color: rgba(255, 255, 255, 1);
        text-align: center;
        z-index: 999;

    }

    .image-dialog-animate {
        display: none;
        position: absolute;
        -webkit-transform-origin: left top;
        transform-origin: left top;
    }

    .image-dialog-animate.loading {
        display: block;
    }

    .dialog-enter-active,
    .dialog-leave-active {
        -webkit-transition: background-color 300ms ease-out;
        transition: background-color 300ms ease-out;
    }

    .dialog-enter,
    .dialog-leave-to {
        background-color: rgba(255, 255, 255, 0);
    }

    .dialog-enter-active .image-dialog-animate,
    .dialog-leave-active .image-dialog-animate {
        display: block;
        -webkit-transition: -webkit-transform 300ms cubic-bezier(1, 0, 0.7, 1);
        transition: -webkit-transform 300ms cubic-bezier(1, 0, 0.7, 1);
        transition: transform 300ms cubic-bezier(1, 0, 0.7, 1);
    }

    .dialog-enter-active .image-dialog-full,
    .dialog-leave-active .image-dialog-full {
        visibility: hidden;
    }

    .image-dialog-background {
        .title {
            display: flex;
            align-items: center;
            flex: 1;
            justify-content: start;
            .placeholder {
                font-size: 0.28rem;
                color: #fff;
                margin-left: 0.12rem;
            }

            .icon-party {
                img {
                    width: 0.5rem;
                    height: 0.5rem;
                    margin-left: 0.05rem;
                }
            }
        }
        .search_box {
            width: 100%;
            height: 0.9rem;
            background-color: rgba(249, 249, 249, 1);
            display: flex;
            align-items: center;
            border-radius: 0.1rem;
            justify-content: start;

            .search {
                border: 0;
                font-size: 0.28rem;
                font-weight: 200;
                font-style: normal;
                color: #434343;
                width: 5.45rem;
                height: 0.62rem;
                padding-left: 0.2rem;
                background-color: white;
                border-radius: 0.1rem;
                margin-left: 0.3rem;
            }
            button {
                border: 0;
                border-radius: 9rem;
                color: #fff;
                width: 1.02rem;
                height: 0.58rem;
                background: linear-gradient(135deg, rgba(255, 57, 57, 1) 0%, rgba(255, 57, 57, 1) 0%, rgba(254, 100, 138, 1) 100%, rgba(254, 100, 138, 1) 100%);
                margin-left: 0.2rem;
            }
        }

    }

    .content {
        text-align: left;
    }

    .main-body .pole {
        padding: 0 0.5rem 0 0.2rem;
        li:nth-child(1) {
            padding-top: 0;
        }
        li {
            padding: 0.3rem 0;
            border-bottom: 1px solid rgba(242, 242, 242, 1);
        }
        img {
            width: 100%;
            height: 3.65rem;

        }
        .title {
            margin-top: 0;
        }
        h3 {
            margin-top: 0.36rem;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            white-space: normal;
            text-align: left;
        }
        .detail {
            margin-top: 0.3rem;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            white-space: normal;
            text-align: left;

        }
        .time {
            margin-top: 0.18rem;
            text-align: left;
        }
    }

    .container .main-body {
        height: calc(100vh - 2rem);
    }

    .type {
        height: 0.85rem !important;
        padding: 0 !important;
        display: flex;
        align-items: center;
        font-size: 0.26rem;
        font-weight: 500;
        color:rgb(67,67,67)
    }
    .container .main-body .mui-table-view li:nth-child(1){
        margin-top: 0 !important;
    }
    input::-webkit-input-placeholder {
        color: rgb(215, 215, 215);
        font-size: 0.28rem;
    }
    input:-moz-placeholder {
        color: rgb(215, 215, 215);
        font-size: 0.28rem;
    }
    input:-ms-input-placeholder {
        color: rgb(215, 215, 215);
        font-size: 0.28rem;
    }

</style>